package cloud.seri.auth.web.rest

import cloud.seri.auth.service.DeptService
import cloud.seri.auth.service.dto.DeptDTO
import com.mongodb.MongoWriteException
import io.swagger.annotations.ApiOperation
import net.dreamlu.mica.core.result.R
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.net.URISyntaxException
import javax.validation.Valid

/**
 * REST controller for managing [cloud.seri.auth.domain.Dept].
 */
@RestController
@RequestMapping("/api")
class DeptResource(
    val deptService: DeptService
)
{

    private val log = LoggerFactory.getLogger(this.javaClass)
    @Value("\${jhipster.clientApp.name}")
    private var applicationName: String? = null

    /**
     * `POST  /depts` : Create a new dept.
     *
     * @param deptDTO the deptDTO to create.
     * @return the [ResponseEntity] with status `201 (Created)` and with body the new deptDTO, or with status `400 (Bad Request)` if the dept has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/depts")
    @ApiOperation("新建部门", nickname = "CreateDept")
    fun createDept(@Valid @RequestBody deptDTO: DeptDTO): R<DeptDTO>?
    {
        log.debug("REST 新建部门 : {}", deptDTO)
        if (deptDTO.id != null)
        {
            return R.fail("部门ID已经存在")
        }
        return deptService.save(deptDTO)
    }

    /**
     * `PUT  /depts` : Updates an existing dept.
     *
     * @param deptDTO the deptDTO to update.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the updated deptDTO,
     * or with status `400 (Bad Request)` if the deptDTO is not valid,
     * or with status `500 (Internal Server Error)` if the deptDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/depts")
    @ApiOperation("更新部门", nickname = "UpdateDept")
    fun updateDept(@Valid @RequestBody deptDTO: DeptDTO): R<DeptDTO>?
    {
        log.debug("REST 更新部门 : {}", deptDTO)
        return deptService.save(deptDTO)
    }

    /**
     * `GET  /depts` : get all the depts.
     *
     * @return the [ResponseEntity] with status `200 (OK)` and the list of depts in body.
     */
    @GetMapping("/depts")
    @ApiOperation("获取全部", nickname = "GetDeptsList")
    fun getAllDepts(): List<DeptDTO>
    {
        log.debug("REST 获取全部")
        return deptService.findAll()
    }

    /**
     * `GET  /depts/:id` : get the "id" dept.
     *
     * @param id the id of the deptDTO to retrieve.
     * @return the [ResponseEntity] with status `200 (OK)` and with body the deptDTO, or with status `404 (Not Found)`.
     */
    @GetMapping("/depts/{id}")
    @ApiOperation("获取详细信息", nickname = "GetOneDept")
    fun getDept(@PathVariable id: String): R<DeptDTO>?
    {
        log.debug("REST 获取详细信息 : {}", id)
        val deptDTO = deptService.findOne(id)
        return R.success(deptDTO)
    }

    /**
     * `DELETE  /depts/:id` : delete the "id" dept.
     *
     * @param id the id of the deptDTO to delete.
     * @return the [ResponseEntity] with status `204 (NO_CONTENT)`.
     */
    @DeleteMapping("/depts/{id}")
    @ApiOperation("删除", nickname = "DeleteOneDept")
    fun deleteDept(@PathVariable id: String): R<Void>?
    {
        log.debug("REST 删除部门 : {}", id)
        deptService.delete(id)
        return R.success()
    }

    companion object
    {
        private const val ENTITY_NAME = "dept"
    }
}
